package coreClassLibrary;

public class StringProcessingDemo {
    
    public static void main(String[] args) {
        // 1. String类的使用
        demonstrateString();
        
        // 2. StringBuilder的使用
        demonstrateStringBuilder();
        
        // 3. StringBuffer的使用
        demonstrateStringBuffer();
        
        // 4. 字符串处理方法的使用
        demonstrateStringMethods();
        
        // 5. 性能比较
        comparePerformance();
    }
    
    // String类示例
    private static void demonstrateString() {
        System.out.println("\n=== String类示例 ===");
        
        // 字符串创建
        String str1 = "Hello";  // 字符串字面量
        String str2 = new String("Hello");  // 使用构造函数
        
        // 字符串比较
        System.out.println("str1 == str2: " + (str1 == str2));  // false
        System.out.println("str1.equals(str2): " + str1.equals(str2));  // true
        
        // 字符串不可变性
        String str3 = str1;
        str1 += " World";  // 创建新的字符串对象
        System.out.println("str3: " + str3);  // 原字符串不变
        System.out.println("str1: " + str1);  // 新字符串
    }
    
    // StringBuilder示例
    private static void demonstrateStringBuilder() {
        System.out.println("\n=== StringBuilder示例 ===");
        
        StringBuilder sb = new StringBuilder("Hello");
        
        // 添加内容
        sb.append(" World");
        System.out.println("追加后: " + sb);
        
        // 插入内容
        sb.insert(5, " Dear");
        System.out.println("插入后: " + sb);
        
        // 删除内容
        sb.delete(5, 10);
        System.out.println("删除后: " + sb);
        
        // 反转字符串
        sb.reverse();
        System.out.println("反转后: " + sb);
    }
    
    // StringBuffer示例
    private static void demonstrateStringBuffer() {
        System.out.println("\n=== StringBuffer示例 ===");
        
        StringBuffer sbf = new StringBuffer("Hello");
        
        // 线程安全的操作
        sbf.append(" World");
        System.out.println("追加后: " + sbf);
        
        // 替换操作
        sbf.replace(0, 5, "Hi");
        System.out.println("替换后: " + sbf);
        
        // 容量操作
        System.out.println("当前容量: " + sbf.capacity());
        System.out.println("当前长度: " + sbf.length());
    }
    
    // 字符串处理方法示例
    private static void demonstrateStringMethods() {
        System.out.println("\n=== 字符串处理方法示例 ===");
        
        String text = "  Hello World! This is a String example.  ";
        
        // 基本操作
        System.out.println("长度: " + text.length());
        System.out.println("转大写: " + text.toUpperCase());
        System.out.println("转小写: " + text.toLowerCase());
        System.out.println("去除首尾空格: " + text.trim());
        
        // 查找和替换
        System.out.println("包含'World': " + text.contains("World"));
        System.out.println("替换: " + text.replace("Hello", "Hi"));
        
        // 分割和截取
        String[] words = text.split(" ");
        System.out.println("分割后第一个词: " + words[0]);
        System.out.println("子串: " + text.substring(2, 7));
        
        // 开始和结束判断
        System.out.println("是否以'Hello'开始: " + text.trim().startsWith("Hello"));
        System.out.println("是否以'.'结束: " + text.trim().endsWith("."));
    }
    
    // 性能比较示例
    private static void comparePerformance() {
        System.out.println("\n=== 性能比较 ===");
        
        int iterations = 100000;
        
        // String性能测试
        long startTime = System.currentTimeMillis();
        String str = "";
        for (int i = 0; i < iterations; i++) {
            str += "a";
        }
        long stringTime = System.currentTimeMillis() - startTime;
        
        // StringBuilder性能测试
        startTime = System.currentTimeMillis();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < iterations; i++) {
            sb.append("a");
        }
        long stringBuilderTime = System.currentTimeMillis() - startTime;
        
        // StringBuffer性能测试
        startTime = System.currentTimeMillis();
        StringBuffer sbf = new StringBuffer();
        for (int i = 0; i < iterations; i++) {
            sbf.append("a");
        }
        long stringBufferTime = System.currentTimeMillis() - startTime;
        
        System.out.println("String耗时: " + stringTime + "ms");
        System.out.println("StringBuilder耗时: " + stringBuilderTime + "ms");
        System.out.println("StringBuffer耗时: " + stringBufferTime + "ms");
    }
} 